<?php
// +----------------------------------------------------------------------
// | 新闻信息页面
// +----------------------------------------------------------------------

namespace Home\Controller;
use Common\Controller\Base;
use Common\Model\Model;

class NewsController extends Base {

    //列表页面
    public function index(){
       //获取新闻的广告
        $advert = M('NewsAdvert')->where(array('seat'=>1,'style'=>1))->limit(4)->order('sort asc,create_time desc')->select();
        $advertes = M('NewsAdvert')->where(array('seat'=>2,'style'=>1))->order('create_time desc')->find();

        //推荐新闻
        $news = M('News')->where(array('iftj'=>1))->limit(3)->order('sort desc')->select();

        //活动列表
        $activity = M('Activity')->where(array('status'=>1))->limit(5)->order('sort asc')->select();

        //热门新闻
        $hotNews = M('News')->limit(10)->order('create_time desc')->select();

        //专题
        $special = M('NewsSpecial')->where(array('style'=>1))->limit(6)->order('sort asc')->select();

        $this->assign("advert", $advert);
        $this->assign('advertes',$advertes);
        $this->assign('news',$news);
        $this->assign('class',$this->newsClass());
        $this->assign('activity',$activity);
        $this->assign('hotNews',$hotNews);
        $this->assign('special',$special);
        $this->display();
    }

    //新闻分类页
    public function category(){
        $id = I('get.id');
        $model = M('News');
        $where['cateid2'] = $id;
        $count = $model->where($where)->count();
        $page = $this->page($count, 10);
        $list = $model->where($where)->limit($page->firstRow . ',' . $page->listRows)->order(array('id' => 'DESC'))->select();

        //获取分类信息
        $newsClass = M('NewsClass')->where(array('id'=>$id))->find(); 

        //活动列表
        $activity = M('Activity')->where(array('status'=>1))->limit(5)->order('sort asc')->select();

        //获取新闻的广告
        $advertes = M('NewsAdvert')->where(array('seat'=>3,'style'=>1))->order('create_time desc')->find();

        //热门的新闻
        $news = $model->limit(6)->order("clicknum desc")->select();

        //收藏
        $userid = $this->userid;
        $collction = M('NewsCollection')->where(array('userid'=>$userid,'infoid'=>$id))->find();

        $this->assign("list", $list);
        $this->assign("Page", $page->show());
        $this->assign('activity',$activity);
        $this->assign('advertes', $advertes);
        $this->assign('news',$news);
        $this->assign('newsClass',$newsClass);
        $this->assign('collction',$collction);
        $this->display();
    }

    //详情页面
    public function read(){

        $infoid=I('get.id');
        if(empty($infoid)){
            $this->_empty();
        }

        M('News')->where(array('id'=>$infoid))->setInc('clicknum',1);
        $data = M('News')->where(array('id'=>$infoid))->find();
        //新闻内容进行分页、
        $npage = I('get.npage')?I('get.npage'):1;
        $contents = explode('[page]',$data['content']);
        $PageCount = count($contents); 
        $data['content'] = $contents[$npage-1];
       
        //获取分类
        $class = M('NewsClass')->where(array('id'=>$data['cateid2']))->find();       
      
        //获取新闻的广告
        $advertes = M('NewsAdvert')->where(array('seat'=>3,'style'=>1))->order('create_time desc')->find();

        //热门的新闻
        $news =  M('News')->limit(6)->order("clicknum desc")->select();

         //作品列表
        $works = M('Works')->where(array('type'=>1))->limit(6)->order('clicknum desc')->select();

        //上一篇
        $lastNews =  M('News')->where("id < ".$infoid)->order('id desc')->find();
       
        //下一篇
        $nextNews = M('News')->where("id >".$infoid)->order('id asc')->find();



        //评论
        $where['infoid']=$infoid;
        $where['type']=5;
        $where['parent_id'] = 0;
        $count = M('UserMessage')->where($where)->count();
      
        $limitRows = 10; // 设置每页记录数
        $page = $this->ajaxpage($count,$limitRows,'','ajax_div','ajax_page_div');

        $msgModel=new Model();
        $message=$msgModel
            ->table(array(
                    C('DB_PREFIX').'user_message'=>'M',
                    C('DB_PREFIX').'user'=>'U',
                )
            )
            ->where('M.userid=U.userid and M.status=true and M.infoid='.$infoid.' and M.type=5 and M.parent_id=0')
            ->field('M.create_time,M.content,U.nickname,U.uavatar,U.openuid,U.uprovince,U.ucity,U.userid,M.id,M.touserid')
            ->order('M.create_time DESC')
            ->limit($page->firstRow . ',' . $page->listRows)
            ->select();

        foreach ($message as $key => $value) {
            $ides[] = $value['id'];
        }
        $pid = implode(",",$ides);
        
        $messageTow = M('UserMessage')->alias('M')
                    ->field('M.id,M.create_time,M.content,M.parent_id,M.userid as museid,U.nickname,R.userid,R.uavatar,R.nickname as uname')
                    ->join('LEFT JOIN __USER__ U ON M.touserid=U.userid')
					->join('LEFT JOIN __USER__ R ON M.userid=R.userid')
                    ->where(array("M.type=5 and M.infoid=".$infoid,'parent_id'=>array('IN',$pid)))
                    ->select();
		
        if($messageTow){

            $mesdata = array_merge_recursive($message,$messageTow);

        }else{

            $mesdata = $message;
        }
        $msglist = array();

        foreach ($mesdata as $k => $v) {
            if($v['parent_id'] ==0){

                 $msglist[$v['id']] = $v;

            }else{
                 $msglist[$v['parent_id']]['sou'][] = $v;
               
            }
        }

      
        $this->assign('data',$data);
        $this->assign('advertes',$advertes);
        $this->assign('news',$news);
        $this->assign('PageCount',$PageCount);
        $this->assign('npage',$npage);
        $this->assign('works',$works);
        $this->assign('lastNews',$lastNews);
        $this->assign('nextNews',$nextNews);
        $this->assign('class',$class);
        $this->assign('count',$count);
        $this->assign('msglist',$msglist);
        $this->assign('page',$page->show());
        $this->display();

    }

    //新闻分类
    public function newsClass(){
        $data = M('NewsClass')->where(array('style'=>1))->order('fid asc')->select();
        $newsClass = array();

        foreach ($data as $key => $value) {
            if($value['fid'] == 0){
                $newsClass[$value['id']] = $value;
            }else{
                $newsClass[$value['fid']]['son'][] = $value;
            }
        }

        return $newsClass;
    }

    //新闻收藏

    public function newscoll(){
        $data = I('get.');
        $data['userid'] = $this->userid;
        $this->check();
        $data['type'] = 1;
         $data['create_time'] = time();
         if(M('NewsCollection')->add($data)){
             $this->success('取消收藏','news/newscollde/');
        }
    }

    public function newscollde(){
        $data = I('get.');
        $data['userid'] = $this->userid;
        if(M('NewsCollection')->where($data)->delete()){
           $this->success('收藏','news/newscoll/');
        }
    }

}